<template>
	<view class="wap">
		<navbar :parameter='parameter'></navbar>
		<block v-for="(item,index) in list" :key="index">
			<navigator class="item" :url="item.path">
				<image class="pic" :src="item.pic"></image>
				<view class="cont-box">
					<view class="title"> {{item.title}} </view>
					<view class="cont"> {{item.cont}} </view>
				</view>
			</navigator>
		</block>
		<view class="btn-box">
			<view @click="beStaff" class="btn">我要入驻平台</view>
		</view>

		<!-- 提示 -->
		<u-toast ref="uToast"></u-toast>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				parameter: {
					'navbar': '1',
					'return': '1',
					'title': '我要成为陪诊师',
					'color': true,
					'class': 'on'
				},

				list: [{
					path: '/pages/user/video/list?type=' + 1,
					pic: '/static/images/consultation1.jpg',
					title: '线上培训',
					cont: '为就医患者提供寻求专业人员陪同就医、专车接送、排队买药等服务。',
				}, {
					path: '/pages/user/video/list?type=' + 2,
					pic: '/static/images/consultation2.jpg',
					title: '线下培训',
					cont: '为就医患者提供寻求专业人员陪同就医、专车接送、排队买药等服务。',
				}],
				staff_status: '',
			};
		},
		onLoad(option) {
			let pages = getCurrentPages();
			if (pages[pages.length - 2]) {
				//如果有上一页，就返回上一页
				this.parameter.return = '1';
			} else {
				this.parameter.return = '2';
			}
		},
		onShow() {

		},
		/**
		 * 用户点击右上角分享
		 */
		onShareAppMessage: function() {

		},
		onShareTimeline(res) { //分享到朋友圈

		},
		methods: {
			async userInfos(isRefresh) {
				var that = this;
				const res = await this.$myRequest({
					url: '/api/user/userInfo',
					data: {
						openid: uni.getStorageSync('openid')
					}
				})
				this.staff_status = res.data.data.staff_status;
				console.log(this.staff_status);
				if (isRefresh == 1) {
					if (this.staff_status == -1) {
						uni.navigateTo({
							url: "/pages/user/apply_form"
						})
					} else if (this.staff_status == 0) {
						this.$refs.uToast.show({
							type: 'success',
							message: '待审核中',
						})
					} else if (this.staff_status == 1) {
						this.$refs.uToast.show({
							type: 'success',
							message: '您已是陪诊师，请勿重复申请',
						})
					} else if (this.staff_status == 2) {
						this.$refs.uToast.show({
							type: 'error',
							message: '审核失败，请重新审核',
							complete() {
								uni.navigateTo({
									url: "/pages/user/apply_form"
								})
							}
						})
					} else if (this.staff_status == 3) {
						this.$refs.uToast.show({
							type: 'error',
							message: '押金未支付，请支付',
							complete() {
								that.createDepositOrder()
							}
						})
					}
				}
			},
			// 创建押金订单
			async createDepositOrder() {
				var that = this;
				const res = await this.$myRequest({
					url: '/api/order/createDepositOrder',
					data: {
						openid: uni.getStorageSync('openid'),
					}
				})
				console.log('创建押金订单', res.data);
				this.payDepositOrder(res.data.data)

			},
			// 支付押金订单
			async payDepositOrder(id) {
				var that = this;
				const res = await this.$myRequest({
					url: '/api/order/payDepositOrder',
					data: {
						openid: uni.getStorageSync('openid'),
						order_id: id,
					}
				})
				console.log('支付', res.data.data)
				var orderInfo = res.data.data;
				uni.requestPayment({
					"provider": "wxpay", //固定值为"wxpay"
					"timeStamp": orderInfo.timeStamp,
					"nonceStr": orderInfo.nonceStr,
					"package": orderInfo.package,
					"signType": orderInfo.signType,
					"paySign": orderInfo.paySign,
					success: function(res) {
						that.$refs.uToast.show({
							type: 'success',
							message: '支付成功，待审核',
						})
						that.userInfos(2);
					},
					fail: function(err) {
						that.$refs.uToast.show({
							type: 'error',
							message: '支付失败',
							complete() {
								uni.navigateBack()
							}
						})
					}
				});


			},
			beStaff() {
				var openid = uni.getStorageSync('openid');
				var staff_status = this.staff_status;
				var that = this;
				if (openid) {
					that.userInfos(1);

				} else {
					this.$refs.uToast.show({
						type: 'error',
						message: '您尚未登录，请先授权登录',
						complete() {
							uni.switchTab({
								url: "/pages/user/index"
							})
						}
					})
				}

			}
		}
	}
</script>

<style lang="scss" scoped>
	.wap {
		padding: 30rpx;
	}

	.item {
		background-color: #fff;
		margin-bottom: 30rpx;
		position: relative;
		width: 100%;
		height: 288rpx;
	}

	.item .pic {
		width: 100%;
		height: 100%;
	}

	.item .cont-box {
		position: absolute;
		width: 420rpx;
		padding-top: 90rpx;
		padding-left: 30rpx;
		left: 0;
		top: 0;
	}

	.item .cont-box .title {
		position: relative;
		font-size: 32rpx;
		padding-left: 30rpx;
		line-height: 1;
		margin-bottom: 30rpx;
	}

	.item .cont-box .title::after {
		content: "";
		position: absolute;
		top: 0;
		left: 0;
		width: 8rpx;
		height: 100%;
		background: #347DFE;
		border-radius: 3px;
	}

	.item .cont-box .cont {
		font-size: 24rpx;
		color: #999;
		line-height: 1.5;
	}

	.btn-box {
		padding-top: 30rpx;
	}

	.btn-box .btn {
		width: 100%;
		height: 80rpx;
		border-radius: 12rpx;
		line-height: 80rpx;
		text-align: center;
		margin-bottom: 30rpx;
		background-color: #347DFE;
		color: #fff;
		font-size: 30rpx;
	}
</style>
